﻿using Microsoft.AspNetCore.Mvc;
using NET6.WebAPI.Extensions;

namespace NET6.WebAPI.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class LogController : ControllerBase
    {
        private readonly ILogger<LogController> _log;

        public LogController(ILogger<LogController> logger)
        {
            _log = logger;
        }

        [HttpGet]
        public IActionResult Get() 
        {
            try
            {
                _log.LogInformation("记录一个信息日志：{msg}", "今天是个好日子"); // 使用模板字符串性能更好

                throw new Exception("突然发生了一个异常");
            }
            catch (Exception ex)
            {
                // 转 JSON 后路径只会显示一个斜杠
                _log.LogError("记录一个异常日志：{info}", new { msg = ex.Message, site = ex.StackTrace }.ToJson());

                throw;
            }
        }
    }
}
